package com.itany.mybatis;

import com.itany.mybatis.dao.HeroTypeDao;
import com.itany.mybatis.entity.HeroType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Description: 类的描述
 * Author: liudaolin@itany.com
 * Date: 2021/07/20 16:14
 * Version: 1.0 版本信息
 */
public class Test04 {
    private SqlSessionFactory factory;
    private SqlSession session;
    @Before
    public void before() {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        factory = builder.build(Test01.class
                .getClassLoader()
                .getResourceAsStream("mybatis-config.xml"));
        session = factory.openSession();
        System.out.println("start");
    }
    @After
    public void after() {
        if(null != session){
            session.close();
        }
        System.out.println("end");
    }

    @Test
    public void f1() {
        HeroTypeDao mapper = session.getMapper(HeroTypeDao.class);
        HeroType ht1 = mapper.selectByTypeId(1);
        System.out.println(ht1);
        HeroType ht2 = mapper.selectByTypeId(2);
        System.out.println(ht2);
        System.out.println("----------------------");
        // 相同的方法, 相同的参数, 默认会先从缓存中查询
        HeroType ht11 = mapper.selectByTypeId(1);
        System.out.println(ht11);
        // 执行 commit, 会 清空 一级缓存
        session.commit();

        System.out.println("----------------------");
        HeroType ht12 = mapper.selectByTypeId(1);
        System.out.println(ht12);
    }

    @Test
    public void f2() {
        HeroTypeDao mapper = session.getMapper(HeroTypeDao.class);

        List<HeroType> list = mapper.selectAll1();
        list.forEach(System.out::println);
        System.out.println("-----------------");

        HeroType ht1 = mapper.selectByTypeId(1);
        System.out.println(ht1);
    }

    @Test
    public void f3() {
        SqlSession s1 = factory.openSession();
        HeroTypeDao mapper = s1.getMapper(HeroTypeDao.class);
        HeroType ht1 = mapper.selectByTypeId(1);
        System.out.println(ht1);

        // 一定要 commit, 否则数据不会被 (二级缓存) 缓存, 被缓存的 数据类型 一定要实现 Serializable
        s1.commit();

        System.out.println("----------------------");

        SqlSession s2 = factory.openSession();
        HeroTypeDao mapper2 = s2.getMapper(HeroTypeDao.class);
        HeroType ht2 = mapper2.selectByTypeId(1);
        ht2.setName("qqqq");
        System.out.println(ht2);
        s2.commit();

        System.out.println("----------------------");

        SqlSession s3 = factory.openSession();
        HeroTypeDao mapper3 = s3.getMapper(HeroTypeDao.class);
        HeroType ht3 = mapper3.selectByTypeId(1);
        System.out.println(ht3);
        s3.commit();

        System.out.println("----------------------");
        System.out.println(ht2 == ht3);

    }


}
